<template>
	<view>
		<view class="u-m-t-20">
			<u-cell-group>
				<u-cell-item  :title="campusStr" :arrow="false" class="info">
						<u-switch slot="right-icon" v-model="isHaiZhu" :loading="switchLoading" @change="switchChange"></u-switch>
				</u-cell-item>
			</u-cell-group>
			<br>
			<p class="text">该设置将决定您的课表时间以及上课推送时间</p>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				campusStr:'',
				switchLoading: false,
				isHaiZhu:getApp().globalData.isHaiZhu
			}
		},
		async onLoad() {
			let vm = this
			if(vm.isHaiZhu == true){
				vm.campusStr = "当前校区：海珠校区"
			}else{
				vm.campusStr = "当前校区：白云校区"
			}
		},
		methods: {
			async switchChange(){
				let vm = this
				console.log(vm.isHaiZhu)
				let res = await uni.showModal({
					title: vm.isHaiZhu ? '是否切换为海珠校区？' : '是否切换为白云校区？',
					cancelText:"否",
					confirmText:"是"
				})
				if (res[1].cancel== true){
					console.log('取消')
					vm.isHaiZhu = !vm.isHaiZhu;
				}else if (res[1].confirm == true){
					console.log('确定')
					vm.switchLoading = true
					let body = new Object()
					let header = new Object()
					let authority = uni.getStorageSync("Authority")
					header.Authority = authority
					vm.header = header
					if(vm.isHaiZhu){
						body.campusNo = 1
					}else{
						body.campusNo = 2
					}
					let deleteCache = await getApp().UniRequest("/user/authority/justDeleteCache","GET","",header)
					console.log(deleteCache)
					let data = await getApp().officailRequest("/officialUser/authority/changeCampus","POST",body,header)
					if (data.code == 20000){
						uni.showToast({
							title: '设置成功!',
						})
						getApp().globalData.isHaiZhu = !getApp().globalData.isHaiZhu
					}else {
						uni.showToast({
							title: '设置失败!',
							icon: 'error'
						})
						vm.isHaiZhu = !vm.isHaiZhu;
					}
					if(vm.isHaiZhu){
						vm.campusStr = "当前校区：海珠校区"
					}else{
						vm.campusStr = "当前校区：白云校区"
					}
					vm.switchLoading = false
				}
			}
		}
	}
</script>

<style>
.buttonCss{
	color: #000000;
}
.text{
	text-align: center;
	color: #c0c4cc;
	position: relative;
	top: 20rpx;
}
.text{
	text-align: center;
	color: #c0c4cc;
	position: relative;
	top: 20rpx;
}
</style>
